<?php
/*
 * File Name : InsertFunds.php
 * Description : Insert the agency's fund raising goals in the database.
 * Created By : Pratibha Awasthi
 */

include_once "basePage.php";

class InsertFunds extends basePage{
	
	// Inserts the fund raiasing goals of a subscriber into the database
	public function insert_funds(){
		
		$frequency = $_POST['frequency'];
		session_start();
		$username = $_SESSION['userid'];
		$agencyid = $_SESSION['agencyid'];
		$start = $_POST['startdate'];
			
		$end = $_POST['enddate'];
		$amount = $_POST['amount'];
		$fundname = $_POST['fundname'];
		
		// Check if the values entered in the form on the previous page are valid or Null
		if($fundname== NULL || $frequency == '0' || $start == NULL || $end == NULL || $amount == NULL || $username == NULL){
				echo "<h3>Please enter all the details required setting Fund Raising goals.</h3>";
				echo "<input type = 'submit' name = 'submit' value = 'Back' onclick= 'history.go(-1);'>";
		}
		
		// If user has entered all the required fields
		else{
				$con = mysql_connect('localhost','pa_user','secret_password');
				if (!$con){
		  			die('Could not connect: ' . mysql_error());
		  		}
			    
		  		// Get the number of existing rows in the table in order to calculate the row count 
				$rowcount_result = mysql_query("SELECT * FROM TGS_GM_RESERVATION.tblagencyfundraisinggoals");
				$num_rows = mysql_num_rows($rowcount_result);
				
				$rowno = $num_rows + 1;
				
				
				$start_timestamp = strtotime($start);
				$start_timestamp = date("Y-m-d H:i:s", $start_timestamp);
				
				$end_timestamp = strtotime($end);
				$end_timestamp = date("Y-m-d H:i:s", $end_timestamp);
				
				mysql_select_db("TGS_GM_RESERVATION", $con);
				// Query to insert into the database				
				mysql_query("INSERT INTO TGS_GM_RESERVATION.tblagencyfundraisinggoals(FundraisingGoalsID, Name, StartDate, EndDate, Dollars, tblAgency_ID) VALUES ({$rowno}, '{$fundname}', '{$start_timestamp}', '{$start_timestamp}', '{$amount}', '{$agencyid}')");
				
				echo "<h3> Congratulations !!!! <br/> You have successfully subscribed to the Pledge based donation !!!!</h3>";
					
				
		}
		echo "<br/> <input type = 'submit' name = 'submit' value = 'Back' onclick= 'history.go(-1);'>";
			
	}
}


$page = new InsertFunds();
$page->header();
$page->top_menu_agency();
$page->insert_funds();
$page->footer();
?>